package com.hdyg.csc.mvp.base;

import android.content.Intent;
import android.widget.Toast;

import com.hdyg.csc.activity.common.BaseActivity;
import com.hdyg.csc.app.MvpApplication;
import com.hdyg.csc.util.JsonUtil;
import com.hdyg.csc.util.LogUtils;
import com.hdyg.csc.util.SPUtils;
import com.hdyg.csc.util.okhttp.CallBackUtil;
import com.hdyg.csc.util.okhttp.OkhttpUtil;

import org.json.JSONObject;

import java.io.File;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by Administrator on 2018/3/10.
 */

public class BaseModel {

    private String status_code, message;

    public void load(String url, Map map, final String type, final Class<?> clazz, final OnCallBackListener listener) {
        LogUtils.d("请求参数" + type + "===" + map.toString());
        OkhttpUtil.okHttpPost(url, map, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {
                Toast.makeText(MvpApplication.getContext(), "请求失败，请重试！", Toast.LENGTH_SHORT).show();
                listener.onError();
            }

            @Override
            public void onResponse(String response) {
                LogUtils.d("成功回调" + type + "===" + response);
                if (response == null) {
                    listener.onError();
                } else {
                    Object result = null;
                    try {
                        JSONObject temp = new JSONObject(response);
                        status_code = temp.getString("status");
                        message = temp.getString("message");
                        if (BaseUrl.UN_TOKEN.equals(status_code)) {//未登录
                            SPUtils.put(MvpApplication.getContext(), SpMsg.TOKEN, "");//清除登录状态
                            Intent intent = new Intent(BaseActivity.UN_TOKEN_BROCAST);
                            MvpApplication.getContext().sendBroadcast(intent);
                            return;
                        } else if (status_code.equals(BaseUrl.STATUS)) {//成功
                            result = JsonUtil.parseJsonWithGson(response, clazz);
                            listener.onSuccess(type, result);
                        } else {//失败
                            Toast.makeText(MvpApplication.getContext(), message, Toast.LENGTH_SHORT).show();
                            listener.onError();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(MvpApplication.getContext(), "method=" + type + "," + e.toString(), Toast.LENGTH_SHORT).show();
                        listener.onError();
                    }

                }
            }
        });
    }

    /**
     * 加载 文件格式
     *
     * @param url
     * @param map
     * @param type
     * @param clazz
     * @param listener
     */
    public void loadImageFile(String url, File file, Map map, final String type, final Class<?> clazz, final OnCallBackListener listener) {
        LogUtils.d("请求参数" + type + "===" + map.toString());
        OkhttpUtil.okHttpUploadFile(url, file, "img", "image", map, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {
                Toast.makeText(MvpApplication.getContext(), "请求失败，请重试！", Toast.LENGTH_SHORT).show();
                listener.onError();
            }

            @Override
            public void onResponse(String response) {
                LogUtils.d("成功回调" + type + "===" + response);
                if (response == null) {
                    listener.onError();
                } else {
                    Object result = null;
                    try {
                        JSONObject temp = new JSONObject(response);
                        status_code = temp.getString("code");
                        message = temp.getString("msg");
                        if (BaseUrl.UN_TOKEN.equals(status_code)) {//未登录
                            SPUtils.put(MvpApplication.getContext(), SpMsg.TOKEN, "");//清除登录状态
                            Intent intent = new Intent(BaseActivity.UN_TOKEN_BROCAST);
                            MvpApplication.getContext().sendBroadcast(intent);
                            return;
                        } else if (status_code.equals(BaseUrl.STATUS)) {
                            result = JsonUtil.parseJsonWithGson(response, clazz);
                            listener.onSuccess(type, result);
                        } else {
                            Toast.makeText(MvpApplication.getContext(), message, Toast.LENGTH_SHORT).show();
                            listener.onError();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(MvpApplication.getContext(), "method=" + type + "," + e.toString(), Toast.LENGTH_SHORT).show();
                        listener.onError();
                    }

                }
            }
        });
    }
}
